module Data_cfoam
!
!    include 'mpif.h'

    character*128 :: fname = 'zai.KCAS'

    real(8) :: ZeroEpsilon = 1.0d-10

!    real(8) :: ttime ! Current Time
!    real(8) :: dt ! time step
!    integer :: nstep ! number of Time Step
!    integer :: nStepInitial ! initial number of Time Step, != 1 after Restart
!    real(8) :: dtOut 
!    real(8) :: dt2
    
    integer :: irestart

	logical :: RestartBin = .true. ! .false. ! .true. = binary format, .false. = text format of restart data

    logical :: JustMeshCellFaceCentersOut = .false.
    logical :: CalcBoundaryFaceCubesOnly = .false. ! Compute boundary face numbers
    logical :: OutputFaceCubesOnly = .false. ! output Boundary Face Numbers
    logical :: NewPeriodicNumeration = .false. ! define periodic boundaries
    integer :: PeriodicBound1 = 0
    integer :: PeriodicBound2 = 0
    logical :: SplitPeriodicBoundaries = .false. ! split periodic boundaries
    
    logical :: Boundary2DmeshOut = .true. ! print 2D mesh in normal direction to a boundary
    logical :: PreprOnly = .false.
    logical :: nonReflectOutletBC = .true.
    
    logical :: writeInitCondOn = .false.
    
    logical :: NormalChars = .true. ! .false.

    integer :: nprint ! step of output

    real(8) :: epsilon ! numerical dumping (viscosity)

    ! Files for output
    integer :: outResults,outRestarts, outFW
    integer :: nResOut = 1
    character(7) :: String7Number
    logical :: autoRestart = .true.

    real(8) :: UFON,VFON,WFON,ROFON,PFON,TFON ! infinity variables

	integer :: btype(100)
	
	logical :: TotalPresTempGiven =.false. ! Total pressure and temperature are given for inlet
	                                       ! velocity is given for outlet

	! variables
	! from old time layer
	real(8),allocatable :: p(:) ! pressure in cells
	real(8),allocatable :: T(:) ! temp in cells
	real(8),allocatable :: U(:,:) ! momentum, U, U(3), m/s
	real(8),allocatable :: rho(:)           ! density
	real(8),allocatable :: E(:)             ! full energy, E, m^2/s^2

	! from new time layer
	real(8),allocatable :: pnew(:)
	real(8),allocatable :: Tnew(:)
	real(8),allocatable :: Unew(:,:)
	real(8),allocatable :: rhonew(:)        !
	real(8),allocatable :: Enew(:)          !

	! s - surface, nonconservative
	! from old time layer
	real(8),allocatable :: Ps(:)  ! 
	real(8),allocatable :: Ts(:)
	real(8),allocatable :: Us(:,:)    ! velocity
	real(8),allocatable :: Rhos(:)          ! 
	real(8),allocatable :: Es(:)            ! energy

	! new - new values in faces
	real(8),allocatable :: Psnew(:)
	real(8),allocatable :: Tsnew(:)
	real(8),allocatable :: Usnew(:,:)       ! m/s
	real(8),allocatable :: Rhosnew(:)       !
	real(8),allocatable :: Esnew(:)         !
	real(8),allocatable :: VarMem(:)

	real *8 utmp(8,100) !, utmp1(800) ! temperary variable
	
	real *8,pointer::gradU(:,:,:)       ! tensor diff velocities Dv_i/Dx_j

	real(8) :: cfl ! Curant
    
    ! Physical parameters
!    real(8) :: cv = 717.65d0 ! conductivity
    real(8) :: gam = 1.4d0 ! 1.4 gamma


    integer,pointer::pointProcAddressing(:)

	real(8),pointer :: sendVPoints(:), recvVPoints(:)               ! for sending Volume between Points from diff procs
    integer,pointer :: sendCountsVPoints(:), recvCountsVPoints(:)
    integer,pointer :: sDisplsVPoints(:), rDisplsVPoints(:)

	real(8),pointer :: sendVPoints9(:), recvVPoints9(:)               ! for sending gradU(3,3,nPoints) between Points from diff procs
    integer,pointer :: sendCountsVPoints9(:), recvCountsVPoints9(:)
    integer,pointer :: sDisplsVPoints9(:), rDisplsVPoints9(:)

    real(8) :: meshFactor(3) = 1d0 ! x*meshFactor(1), y*meshFactor(2), z*meshFactor(3)

    integer :: ierr
	integer :: rank
	integer :: nump ! number of processors
!    integer :: nPoints,nCells,nFaces,nInternalFaces,nBoundaryFields ! number of Points, Cells, Faces, InternalFaces,BoundaryFields in mesh
    
    integer :: nodeInterface
	integer,pointer::nodeBoundaryMeshLabel(:) ! list of control volumes on the boundary
	
	!outputfromAllProcs
!	integer :: nCellsGlobal
!	integer :: nFacesGlobal
	real(8),pointer :: recvCell(:), recvFace(:) ! for sending Volume between Points from diff procs
    integer,pointer :: recvCountsCell(:)
    integer,pointer :: recvCountsFace(:)
    integer,pointer :: rDisplsCell(:)
    integer,pointer :: rDisplsFace(:)

    logical :: OutputFromOneProc = .false. ! .true.
	
	
	! Mesh
	! Coors
!	real *8,pointer::meshPoints(:,:)    ! node's coor
	
!	real *8,pointer::meshCellCenters(:,:) ! coord of CellCenters
	! Connectivities
!	integer,pointer::meshCellPoints(:,:) ! connectivity between cells and nodes (1:8,nCells)
	
	! MPI
	integer :: ie
	character *14 processor
    
    ! MPI, for exchanges:
    integer,allocatable :: sendCounts(:)
    integer,allocatable :: sDispls(:)
    integer,allocatable :: recvCounts(:)
    integer,allocatable :: rDispls(:)
    integer,allocatable :: sendCountsv(:)
    integer,allocatable :: sDisplsv(:)
    integer,allocatable :: recvCountsv(:)
    integer,allocatable :: rDisplsv(:)
    
	real(8),allocatable :: send(:)
	real(8),allocatable :: recv(:)
	real(8),allocatable :: sendp(:)
	real(8),allocatable :: recvp(:)
	
	
    ! for Acoustics
    !logical :: AcousticsOnOff = .true. ! .false. ! switch on/off acoustics post-processor
    logical :: AcousticsOnOff = .false. ! .false. ! switch on/off acoustics post-processor

    integer :: nUnusedSections=0 ! number of sections with more than 4 points, for acoustics surface
    integer :: nUnusedSectionsSum=0 ! number of sections with more than 4 points from all processors, for acoustics surface
    integer :: nCellsOut
    integer,allocatable :: meshCellOut(:)
    real(8),allocatable :: xOut(:,:)
    real(8),allocatable :: normalOut(:,:)
    real(8),allocatable :: areaOut(:)

	real(8) :: sound ! sound's velocity
    real(8) :: Pobs(300000)
    real(8),allocatable :: Pobsw(:)
    integer :: nValues ! number of disturbed elements in Pobs
    
    ! Tuning
    integer :: nTimeStepAcceleration = 20000
    integer :: nTimeStepAccelerationStart = 0


    logical :: PS90 = .true. ! .false. ! switch on/off non-uniform boundary inlet condition for nozzle
    
    real(8) :: epsilonPanikovski = 0.0d0
    real(8) :: epsMaximumPrinciple = 0d0
    real(8) :: epsilonSmoothed
    real(8) :: propor ! SK PROPOR AND NTIMEMAX ARE THE NEW VARIABLES TO SLOWLY START FROM THE BACKGROUND FLOW

! Flags for tests
    logical :: InitCondCalc3D = .false. ! .true. !flag for 
    logical :: InitCondHankelFunc2D = .false. ! .true. !  
    logical :: InitCond1Dwaves = .false. !.true. 
    logical :: Benchmark_CAA2 = .false. ! .true. !
    logical :: GaussVelocityProfile = .false. ! .true.

    logical :: ViscPartOnly = .false. ! .true. !
    logical :: ViscPartOnlyX = .false. ! .true.
    real(8) :: Pin,Pout
    
    logical :: IsentropicVortex = .false. ! .true. !
    
    real(8),allocatable :: sumViscousCellFluxes(:,:)

end module Data_cfoam

module DataCells

    logical,allocatable :: CharsCellPresent(:) ! (nCells)
    real(8),allocatable :: SubStepCellVars(:,:) ! (5,nCells):
                                                ! 1:5 = Rho, Velocities, Energy

    real(8),allocatable :: sumCellFluxes(:,:) ! (5,nCells), 1:5 = Rho, Velocities, Energy
    logical,allocatable :: fluxesCellPresent(:) ! (nCells)

    logical,allocatable :: CellGlobalStepDone(:) ! (nCells)
    
    real(8),allocatable :: CurrentVelocities(:,:)
    
end module DataCells

module DataTurbVisc

    logical :: TurbViscCalc = .false.
    
    integer :: nCellsTurbViscCalc
    integer :: nFacesTurbViscCalc
    
    logical,allocatable :: CellTurbViscCalc(:)
    logical,allocatable :: FaceTurbViscCalc(:)
    
    real(8),allocatable :: ViscStressMP(:,:,:) ! viscous stress defined without maximum principle
    real(8),allocatable :: VelocityMP(:,:)     ! velocity defined without maximum principle

end module DataTurbVisc

module DataViscosity

    logical :: CalcViscosityOn = .true. ! .false. !
    
    logical,allocatable :: CellCalcExtrapolatedVars(:)
    logical,allocatable :: PointCalcVisc(:)
    logical,allocatable :: FaceCalcVisc(:)

end module DataViscosity
